package com.apobates.jforum2.troll.forum.mapper;

import com.apobates.jforum2.troll.forum.entity.BoardStats;
import com.apobates.jforum2.troll.forum.stub.vo.BoardStatsVo;
import com.apobates.jforum2.troll.utils.core.DateTimeUtils;
import com.apobates.jforum2.troll.utils.core.api.OptionalEntityTransformMapper;
import com.apobates.jforum2.troll.utils.lang.TriFunction;
import java.util.Optional;
import java.util.function.Function;

/**
 *
 * @author xiaofanku
 */
public class BoardStatsVoMapperEntity implements OptionalEntityTransformMapper<BoardStats, BoardStatsVo>{
    public static TriFunction<Long,Long,Integer,String> produceLink = (Long topicId, Long boardId, Integer boardgroupId)->{
        return String.format("/topic/%d-%d-%d.xhtml", topicId, boardId, boardgroupId);
    };
    
    @Override
    public Optional<BoardStatsVo> transform(Optional<BoardStats> boardStats) {
        return boardStats.map(bs->{
            return mapper().apply(bs);
        });
    }

    public static Function<BoardStats,BoardStatsVo> mapper(){
        return boardStats -> {
            BoardStatsVo bsv = new BoardStatsVo();
            bsv.setBoardId(boardStats.getBoardId());
            bsv.setBoardGroupId(boardStats.getVolumesId());
            bsv.setThreads(boardStats.getTopices());
            bsv.setReplies(boardStats.getPostses());
            bsv.setFavorites(boardStats.getFavorites());
            //
            bsv.setRecentTopic(boardStats.getRecentTopicId());
            bsv.setRecentTopicTitle(boardStats.getRecentTopicTitle());
            bsv.setRecentTopicDate(boardStats.getRecentTopicDate());
            String recentLink = produceLink.apply(boardStats.getRecentTopicId(), boardStats.getBoardId(), boardStats.getVolumesId());
            bsv.setRecentTopicLink(recentLink);
            bsv.setRecentTopicMember(boardStats.getRecentTopicMemberId());
            bsv.setRecentTopicMemberNickname(boardStats.getRecentTopicMemberNickname());
            bsv.setTodayTopics(boardStats.getTodayTopices());
            bsv.setRecentTopicDateString(DateTimeUtils.formatClock(boardStats.getRecentTopicDate()));
            return bsv;
        };
    }
}